package com.ncr.travel.tas.configuration.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import com.ncr.travel.tas.shared.configuration.ConfigurationNodeEnum;
import com.ncr.travel.tas.shared.configuration.ConfigurationProductKey;

/*
@org.hibernate.annotations.Table(
	    name="Forest",
	    indexes = { @Index(name="idx", columnNames = { "name", "length" } ) }
	    )
	    */
@Entity
@Table(name="configuration_node")
public class ConfigurationNode implements Serializable, ConfigurationModelObject {


	/**
	 *
	 */
	private static final long serialVersionUID = 3L;


	private long id;

	private String name;

	private String description;

	private Date deleted;

	private String updatedBy;

	private Date updatedOn;

	private ConfigurationNodeEnum typeEnum;


	private Date version;


	private Set<ConfigurationValue> configurations = new HashSet<ConfigurationValue>();

	public ConfigurationNode(ConfigurationNodeEnum typeEnum){

		parseEnum(typeEnum);
	}

	public ConfigurationNode(){

	}

	private void parseEnum(ConfigurationNodeEnum typeEnum) {
		this.typeEnum = typeEnum;
	}


	public void setId(long id) {
		this.id = id;
	}

	@Id
	@GeneratedValue
	@Column(name="node_id")
	public long getId() {
		return id;
	}

	public void setName(String description) {
		this.name = description;
	}

	public void setName(ConfigurationProductKey configurationKey){
		this.name = configurationKey.name();
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setDeleted(Date deleted) {
		this.deleted = deleted;
	}

	public Date isDeleted() {
		return deleted;
	}

	public void setVersion(Date version) {
		this.version = version;
	}

	@Version
	public Date getVersion() {
		return version;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}

	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedOn(Date date) {
		this.updatedOn = date;
	}

	public Date getUpdatedOn() {
		return updatedOn;
	}


	public void setConfigurations(Set<ConfigurationValue> configurations) {
		this.configurations = configurations;
	}

	@OneToMany(targetEntity=ConfigurationValue.class,
				cascade=javax.persistence.CascadeType.ALL,
				fetch=FetchType.EAGER,orphanRemoval=true)
	@JoinColumn(name="node_id")
	public Set<ConfigurationValue> getConfigurations() {
		return configurations;
	}


	@Enumerated(EnumType.ORDINAL)
	@Column(name="node_type_id")
	public ConfigurationNodeEnum getTypeEnum() {
		return typeEnum;
	}

	public void setTypeEnum(ConfigurationNodeEnum typeEnum) {
		this.parseEnum(typeEnum);
	}


	@Transient
	public void addTable(TableValue table) {
		table.setParent(this);

		this.setConfigurations(table.childValues);
		//this.getConfigurations().add(table);
	}


	public com.ncr.travel.tas.configuration.model.TableValue getTable(String string) {

		for(ConfigurationValue v : this.getConfigurations()){

			if(v.getKeyName().toLowerCase().contains(string.toLowerCase())){
				return new TableValue(v);
			}
		}

		return null;
	}


	/**
	 * adds a configuration value. Can be overwritten by setConfiguration(...)
	 * @param value
	 */
	public void addConfiguration(ConfigurationValue value) {

		this.configurations.add(value);

	}


}
